package com.course01.orderbackend.controller;

import com.course01.orderbackend.dto.OrderDTO;
import com.course01.orderbackend.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    private final OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 创建订单
     * 使用@Valid注解触发请求体的校验
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createOrder(@RequestBody OrderDTO orderDTO) {
        // 校验通过后，调用服务层创建订单
        String orderId = orderService.createOrder(orderDTO);

        // 构建成功响应
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "订单创建成功");
        response.put("data", Map.of("orderId", orderId));

        return new ResponseEntity<>(response, HttpStatus.CREATED);
    }
}
